using System;
using System.Collections.Generic;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace pginstaller.tasks
{
    public class TranslateWix : Task
    {
        [Required]
        public string Source
        {
            get { return _source; }
            set { _source = value; }
        }

        [Required]
        public string Target
        {
            get { return _target; }
            set { _target = value; }
        }

        [Required]
        public string Language
        {
            get { return _language; }
            set { _language = value; }
        }

        private string _source = "", _target = "", _language = "";

        /* Perform the actual build task */
        public override bool Execute()
        {
            Dictionary<string, string> transmap = new Dictionary<string, string>();

            XmlDocument transDoc = new XmlDocument();
            transDoc.Load(_language);
            for (int i = 0; i < transDoc.DocumentElement.ChildNodes.Count; i++)
            {
                if (transDoc.DocumentElement.ChildNodes[i].NodeType != XmlNodeType.Element)
                    continue;
                if (transDoc.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("translation").Value == "")
                    continue;
                if (transmap.ContainsKey(transDoc.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("original").Value))
                    continue;
                transmap.Add(transDoc.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("original").Value,
                             transDoc.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("translation").Value);
            }

            NameTable nt = new NameTable();
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(_source);
            XmlNamespaceManager xns = new XmlNamespaceManager(xDoc.NameTable);
            xns.AddNamespace("w", "http://schemas.microsoft.com/wix/2006/wi");

            Log.LogMessage("Loading {0}", _source);
            if (xDoc.DocumentElement.NodeType == XmlNodeType.Element &&
                xDoc.DocumentElement.Name == "Wix")
            {
                xDoc.DocumentElement.SelectSingleNode("w:Product", xns).Attributes.GetNamedItem("Language").Value = transDoc.DocumentElement.Attributes.GetNamedItem("language").Value;
                xDoc.DocumentElement.SelectSingleNode("w:Product/w:Package", xns).Attributes.GetNamedItem("Languages").Value = transDoc.DocumentElement.Attributes.GetNamedItem("language").Value;
                xDoc.DocumentElement.SelectSingleNode("w:Product", xns).Attributes.GetNamedItem("Codepage").Value = transDoc.DocumentElement.Attributes.GetNamedItem("codepage").Value;
                xDoc.DocumentElement.SelectSingleNode("w:Product/w:Property[@Id='UILANG']", xns).Attributes.GetNamedItem("Value").Value = transDoc.DocumentElement.Attributes.GetNamedItem("language").Value;
            }

            // Translate path to RTF file
            XmlNode n = xDoc.SelectSingleNode("Include/Dialog[@Id='IntroductionDlg']/Control[@Id='welcome_rtf']/Text");
            if (n != null)
                n.Attributes.GetNamedItem("SourceFile").Value = "lang/" + transDoc.DocumentElement.Attributes.GetNamedItem("name").Value + "/welcome.rtf";

            WixProcessor.ProcessWixNode(xDoc.DocumentElement, (WixProcessor.NodeProcessor)delegate(string s) {
                if (transmap.ContainsKey(s))
                    return transmap[s];
                else
                    return s;
            });

            xDoc.Save(_target);
            return true;
       }

   }
}